package yanyan.com.tochar.adpter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


import java.util.List;

import yanyan.com.tochar.R;
import yanyan.com.tochar.beans.CommonGridViewBean;

public class CommonGridViewAdapter extends BaseAdapter {

    private List<CommonGridViewBean> listBeans;
    private LayoutInflater inflater;


    public CommonGridViewAdapter(Context context, List<CommonGridViewBean> listBeans) {
        this.listBeans = listBeans;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return listBeans.size();
    }

    @Override
    public Object getItem(int position) {
        return listBeans.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
//        if(convertView==null) {
//            convertView=inflater.inflate(R.layout.item_common_grid,null);
//            ImageView imageView = (ImageView) convertView.findViewById(R.id.grid_image);
//            TextView text= (TextView) convertView.findViewById(R.id.grid_text);
//            CommonGridViewBean commonGridViewBean = listBeans.get(position);
//            imageView.setImageResource(commonGridViewBean.getImage());
//            text.setText(commonGridViewBean.getTitle());
//            if(commonGridViewBean.getTextColor()!=null){
//                text.setTextColor(commonGridViewBean.getTextColor());
//            }
//
//        }
//        return convertView;
        ViewHolder viewHolder;
        if(convertView==null) {
            viewHolder=new ViewHolder();
            convertView=inflater.inflate(R.layout.item_common_grid,null);
            viewHolder.imageView=(ImageView) convertView.findViewById(R.id.grid_image);
            viewHolder.text=(TextView) convertView.findViewById(R.id.grid_text);
            convertView.setTag(viewHolder);
        }else {
            viewHolder= (ViewHolder) convertView.getTag();
        }
            CommonGridViewBean commonGridViewBean = listBeans.get(position);
            viewHolder.imageView.setImageResource(commonGridViewBean.getImage());
            viewHolder.text.setText(commonGridViewBean.getTitle());
            if(commonGridViewBean.getTextColor()!=null){
                viewHolder.text.setTextColor(commonGridViewBean.getTextColor());
            }
            return convertView;
    }

    class ViewHolder{
        public ImageView imageView;
        public TextView text;
    }
}
